﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SMS.BaseClass;

namespace SMS.GoodsManage
{
    public partial class frmStockInSearch : Form
    {

        #region 常量变量枚举类型定义
        SMS.BaseClass.DataCon datacon = new SMS.BaseClass.DataCon();
        SMS.BaseClass.DataOperate doperate = new SMS.BaseClass.DataOperate();

        private enum COL_NAME : uint
        {
            CHECK = 0,
            STOCK_IN_ID,
            STOCK_IN_DATE,
            STOCK_IN_QTY,
            STOCK_IN_OPERATER,
            STOCK_IN_CONFIRMER,
            STOCK_IN_MEMO
        }
        #endregion 常量变量枚举类型定义

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public frmStockInSearch()
        {
            InitializeComponent();
        }
        #endregion 构造函数

        #region 事件

        #region Form Load
        /// <summary>
        /// Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmStockInSearch_Load(object sender, EventArgs e)
        {
            dgvStockInHead.AutoGenerateColumns = false;
            dgvStockInHead.Controls.Add(hScrollBar);

            this.SetControlStatus();
            this.SearchData();
           
        }
        #endregion Form Load

        #region 新建按钮点击事件
        /// <summary>
        /// 新建按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNew_Click(object sender, EventArgs e)
        {
            SMS.BaseClass.Utils utils = new BaseClass.Utils();
            string strNextStockInID = utils.GetCode(CommonConst.CODE_TYPE_STOCK_IN);
            GoodsManage.frmStockInManage frmStockInManage = new SMS.GoodsManage.frmStockInManage(0, strNextStockInID);
            frmStockInManage.ShowDialog();
            // 子窗体关闭后重新查询
            this.SearchData();

        }
        #endregion 新建按钮点击事件

        #region 变更按钮点击事件
        /// <summary>
        /// 变更按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnModify_Click(object sender, EventArgs e)
        {
            string strSTOCK_IN_ID = string.Empty;
            Boolean flag = false;

            for (int i = 0; i < this.dgvStockInHead.Rows.Count; i++)
            {
                string str = this.dgvStockInHead.Rows[i].Cells[(int)COL_NAME.STOCK_IN_ID].Value.ToString();

                flag = Convert.ToBoolean(((DataGridViewCheckBoxCell)this.dgvStockInHead.Rows[i].Cells[(int)COL_NAME.CHECK]).Value);

                if (flag == true)
                {
                    strSTOCK_IN_ID = this.dgvStockInHead.Rows[i].Cells[(int)COL_NAME.STOCK_IN_ID].Value.ToString();
                    break;
                }
            }

            if (flag)
            {
                GoodsManage.frmStockInManage frmStockInManage = new SMS.GoodsManage.frmStockInManage(1, strSTOCK_IN_ID);
                frmStockInManage.ShowDialog();
                // 子窗体关闭后重新查询
                this.SearchData();
            }
            else
            {
                MessageBox.Show("您没有选择任何入库单！");
            }
        }
        #endregion 变更按钮点击事件

        #region 删除按钮点击事件
        /// <summary>
        /// 删除按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            string strSTOCK_IN_ID = string.Empty;
            Boolean flag = false;

            for (int i = 0; i < this.dgvStockInHead.Rows.Count; i++)
            {
                string str = this.dgvStockInHead.Rows[i].Cells[(int)COL_NAME.STOCK_IN_ID].Value.ToString();

                flag = Convert.ToBoolean(((DataGridViewCheckBoxCell)this.dgvStockInHead.Rows[i].Cells[(int)COL_NAME.CHECK]).Value);

                if (flag == true)
                {
                    strSTOCK_IN_ID = this.dgvStockInHead.Rows[i].Cells[(int)COL_NAME.STOCK_IN_ID].Value.ToString();
                    break;
                }
            }

            if (flag)
            {
                GoodsManage.frmStockInManage frmStockInManage = new SMS.GoodsManage.frmStockInManage(2, strSTOCK_IN_ID);
                frmStockInManage.ShowDialog();
                // 子窗体关闭后重新查询
                this.SearchData();
            }
            else
            {
                MessageBox.Show("您没有选择任何入库单！");
            }
        }
        #endregion 删除按钮点击事件

        #region 查询按钮点击事件
        /// <summary>
        /// 查询按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            // 按条件查询没写
            this.SearchData();
        }
        #endregion 查询按钮点击事件

        #region 输入框回车事件
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ControlOnKeyDown(object sender, KeyEventArgs e)
        {
            // 回车直接查询
            if (e.KeyCode == Keys.Enter)
            {
                this.SearchData();
            }
        }
        # endregion 输入框回车事件

        #region CheckBox单选处理
        /// <summary>
        /// CheckBox单选处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvStockInHead_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1 || e.RowIndex == -1)
            {
                return;
            }

            for (int i = 0; i < this.dgvStockInHead.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)this.dgvStockInHead.Rows[i].Cells[(int)COL_NAME.CHECK];
                checkcell.Value = false;
            }

            DataGridViewCheckBoxCell ifcheck = (DataGridViewCheckBoxCell)this.dgvStockInHead.Rows[e.RowIndex].Cells[(int)COL_NAME.CHECK];
            ifcheck.Value = true;
        }
        #endregion CheckBox单选处理

        #endregion 事件
        
        #region 私有方法

        #region 设置画面控件状态
        /// <summary>
        /// 设置画面控件状态
        /// </summary>
        private void SetControlStatus()
        {
            // 日期CheckBox不选中
            this.cbxData.Checked = false;
            // 日期不可用
            this.dtpSTOCK_IN_DATE.Enabled = false;

            // 明细只读、背景色设置
            this.dgvStockInHead.Columns[(int)COL_NAME.CHECK].ReadOnly = false;
            this.dgvStockInHead.Columns[(int)COL_NAME.CHECK].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_ENABLE;

            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_ID].ReadOnly = true;
            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_ID].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_DATE].ReadOnly = true;
            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_DATE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_QTY].ReadOnly = true;
            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_QTY].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_OPERATER].ReadOnly = true;
            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_OPERATER].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_CONFIRMER].ReadOnly = true;
            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_CONFIRMER].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_MEMO].ReadOnly = true;
            this.dgvStockInHead.Columns[(int)COL_NAME.STOCK_IN_MEMO].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;
        }
        #endregion 设置画面控件状态

        #region 数据查询实行
        /// <summary>
        /// 数据查询实行
        /// </summary>
        private void SearchData()
        {

            // 查询条件
            string strWhere = string.Empty;

            // 入库单号
            if (this.tbxSTOCK_IN_ID.Text.Trim() != string.Empty)
            {
                strWhere += "STOCK_IN_ID LIKE '%" + this.tbxSTOCK_IN_ID.Text.Trim() +"%'";
            }

            // 入库日期
            if (this.dtpSTOCK_IN_DATE.Enabled)
            {
                if (strWhere.Trim() != string.Empty)
                {
                    strWhere += " AND ";
                }
                strWhere += "STOCK_IN_DATE ='" + this.dtpSTOCK_IN_DATE.Value.ToShortDateString() + "'";
            }

            // 操作者
            if (this.tbxSTOCK_IN_OPERATER.Text.Trim() != string.Empty)
            {
                if (strWhere.Trim() != string.Empty)
                {
                    strWhere += " AND ";
                }
                strWhere += "STOCK_IN_OPERATER LIKE '%" + this.tbxSTOCK_IN_OPERATER.Text.Trim() + "%'";
            }

            // 确认者
            if (this.tbxSTOCK_IN_CONFIRMER.Text.Trim() != string.Empty)
            {
                if (strWhere.Trim() != string.Empty)
                {
                    strWhere += " AND ";
                }
                strWhere += "STOCK_IN_CONFIRMER LIKE '%" + this.tbxSTOCK_IN_CONFIRMER.Text.Trim() + "%'";
            }

            if (strWhere.Trim() != string.Empty)
            {
                strWhere = " WHERE " + strWhere;
            }

            DataSet myds = datacon.getds("SELECT STOCK_IN_ID,STOCK_IN_DATE,STOCK_IN_QTY,STOCK_IN_OPERATER,STOCK_IN_CONFIRMER,STOCK_IN_MEMO "
                                       + "FROM T_STOCK_IN_HEAD " + strWhere +" ORDER BY STOCK_IN_ID ASC;", "T_STOCK_IN_HEAD");

            dgvStockInHead.DataSource = myds.Tables["T_STOCK_IN_HEAD"];
            dgvStockInHead.ReadOnly = false;
        }
        #endregion 数据查询实行
        
        #region 入库日期CheckBox选择切换事件（CheckedChanged）
        /// <summary>
        /// 入库日期CheckBox选择切换事件（CheckedChanged）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxData_CheckedChanged(object sender, EventArgs e)
        {
            this.dtpSTOCK_IN_DATE.Enabled = this.cbxData.Checked;
        }
        #endregion 入库日期CheckBox选择切换事件（CheckedChanged）

        #endregion 私有方法
    }
}
